package cn.itwen.star.graph.core.service.impl;

import cn.itwen.star.graph.comfyui.client.pojo.ComfyuiTask;
import cn.itwen.star.graph.core.mapper.UserResultMapper;
import cn.itwen.star.graph.core.pojo.UserResult;
import cn.itwen.star.graph.core.service.UserFundRecordService;
import cn.itwen.star.graph.core.service.UserResultService;
import cn.itwen.star.graph.core.utils.UserUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserResultServiceImpl extends ServiceImpl<UserResultMapper, UserResult> implements UserResultService {


    @Autowired
    private UserFundRecordService userFundRecordService;

    @Override
    @Transactional
    public void insertUserResult(ComfyuiTask task, List<String> urls) {
        List<UserResult> userResultList = urls.stream().map(url -> {
            UserResult userResult = new UserResult();
            userResult.setCreatedTime(LocalDateTime.now());
            userResult.setCollect(0);
            userResult.setUrl(url);
            userResult.setUserId(task.getUserId());
            return userResult;
        }).toList();
        saveBatch(userResultList);

        //积分划扣
        userFundRecordService.pointsDeduction(task.getUserId(), task.getSize());
    }



}
